# -*- coding: utf-8 -*-
"""
Created on Sun Mar 20 09:00:09 2022

@author: 29165
"""
#<span style="font-family: 微软雅黑">12月23日上午，常德高新区举行2021年第四季度重大项目签约仪式，第四季度共引进项目10个，总投资额22亿元。常德高新区党工委副书记、管委会主任曾君主持仪式。</span>
#<span style="font-family: 微软雅黑">12月23日上午，常德高新区举行2021年第四季度重大项目签约仪式，第四季度共引进项目10个，总投资额22亿元。常德高新区党工委副书记、管委会主任曾君主持仪式。</span>
import re
import requests
from bs4 import BeautifulSoup
import time
import pandas as pd
st=time.time()
import docx
import time
from docx import Document   #用来建立一个word对象
from docx.shared import Pt  #用来设置字体的大小
from docx.shared import Inches
from docx.oxml.ns import qn  #设置字体
from docx.shared import RGBColor  #设置字体的颜色
#from docx.enum.text import WD_ALIGN_PARAGRAPH  #设置对其方式
import re
import os
from docx.shared import Cm
def get_url(data,headers):
    htmls=[]
    num=0
    global l,t
    l=list(data.link)
    t=list(data.title)
    for i in l:
        try:
            r=requests.get(i,headers=headers)
            r.raise_for_status()
            r.encoding='utf-8'
            html=r.text
            htmls.append(html)
            time.sleep(1)
            #print(len(htmls))
            #print(htmls)
        except:
            print('error')
    return htmls
def als_text(htmls,data,headers):
    soups=[]
    for html in htmls:
        soup=BeautifulSoup(html,'html.parser')
        soups.append(soup)
    num=0#文件编号
    for soup in soups:
        div=soup.find('div',attrs={'class':'conTxt'})
        #print(div,end='-----------------------------------------\n')
        i=1#图片编号
       
    #-------------------------------------------------------------------------#
        doc=docx.Document()
        doc.styles['Normal'].font.name = u'宋体'
        doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
        print('----------document style{} initialized-------------'.format(num))
        
        title=t[num]
        file_namea=re.sub(r'_x000D_\n',' ',title)
        file_nameb=file_namea.replace(r'|','')
        file_name='{}'.format(file_nameb.replace('_x000D_',''))
        print('----------document name{} initialized-------------'.format(num))
        num+=1
        doc.add_heading(title)
        try:
            for p in div.find_all('p'):
                #print(p)
                if p.find('a') is None:
                    print('text')
                    doc.add_paragraph('    ').add_run(p.string)
                else:
                
                        img=p.find('a').get('href')
                        print(img)
                        r=requests.get('https://cdgxq.changde.gov.cn'+img,headers=headers)
                        f=open(".\图片\{}{}.jpg".format(file_name,i),"wb")
                        f.write(r.content)
                        doc.add_picture(".\图片\{}{}.jpg".format(file_name,i),width=Cm(16))
                        i+=1
        except:
            pass
        doc.save('./加入图片/'+file_name+'.docx')
                
                
def main():
    data=pd.read_excel('内链.xlsx')
    print(1)
    headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36 Edg/99.0.1150.30'}
    htmls=get_url(data, headers)
    print(2)
    als_text(htmls,data,headers)
    print(time.time()-st)    
main()           